package com.liaogui.consumer.shiro.config;

import com.liaogui.consumer.shiro.CustomerRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
/**
 * 描述：
 *
 * @author caojing
 * @create 2019-01-27-13:38
 */
@Configuration
public class ShiroConfig {
    @Autowired
    private CustomerRealm customerRealm;
    //ShiroFilter过滤所有请求
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //给ShiroFilter配置安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //配置系统受限资源
        //配置系统公共资源
        Map<String, String> map = new HashMap<String, String>();
        map.put("/sys/login","anon");//表示这个为公共资源 一定是在受限资源上面
        map.put("/sys/index","anon");
        map.put("/sys/create","anon");
        map.put("http://localhost:8065/myblog/userinfo/update","anon");
        map.put("/myblog/userinfo/list","anon");
        map.put("/myblog/userinfo/delete","anon");
        map.put("/index","authc");//表示这个资源需要认证和授权
        map.put("/myblog/**","authc");


        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        // 设置认证界面路径
        shiroFilterFactoryBean.setLoginUrl("/sys/login");
        return shiroFilterFactoryBean;
    }
    //创建安全管理器
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("customerRealm")CustomerRealm customerRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(customerRealm);
        return securityManager;
    }
    //创建自定义Realm
    @Bean
    public CustomerRealm customerRealm() {
        CustomerRealm realm = new CustomerRealm();
        return realm;
    }
}



